/**
 * Created by matengfei on 15/8/22.
 */


///* OK

window.onload = function(){
    //alert("文档初始化完毕");
    //draw_directory();


    rr();
};

//*/


function draw_directory() { //

    var left = document.getElementById("left");

    var root_ul = document.createElement("ul");
    var root_li = document.createElement("li");
    root_li.innerHTML = "标题1";
    root_ul.appendChild(root_li);

    var root_ul1 = document.createElement("ul");
    var root_li1 = document.createElement("li");    root_li1.innerHTML = "<a class='' href=''>主食</a>";
        var root_ul2 = document.createElement("ul");
        var root_li2 = document.createElement("li");  root_li2.innerHTML = "<a class='' href=''>米饭</a>";
        var root_li3 = document.createElement("li"); root_li3.innerHTML = "<a class='' href=''>面条</a>";
        root_ul2.appendChild(root_li2);
        root_ul2.appendChild(root_li3);
    root_li1.appendChild(root_ul2);
    root_ul1.appendChild(root_li1);

    left.appendChild( root_ul );
    left.appendChild(root_ul1);


}

function init_data(){
    var root = new Object();
    var child1 = new Object();
    child1.text="米饭";
    child1.next=null;

    var child2 = new Object();
    var child2_array = new Array();
    child2.text="面条";
        var child2_1 = new Object();
        child2_1.text="粗";
        child2_1.next=null;
        var child2_2 = new Object();
        child2_2.text="细";
        child2_2.next=null;
    child2_array[0] = child2_1;
    child2_array[1] = child2_2;
    child2.next=child2_array;

    var root_array = new Array();
    root_array[0] = child1;
    root_array[1] = child2;

    root.text = "主食"
    root.next = root_array;


    var json = JSON.stringify(root);
    //alert(json);

    return json;
}


function rr(){

    var left = document.getElementById("left");

    var json_data = init_data();
    var root_ul = document.createElement("ul");
    var obj = JSON.parse(json_data);
    left.appendChild(root_ul);

    parse_data(obj,root_ul);

}

function parse_data(obj,ul){


    var li = document.createElement("li");
    li.innerHTML = "<a class='' href='' >"+obj.text+"</a>";
    ul.appendChild(li);

    if(obj.next == null){
        return;
    }


    for(var i =0; i<obj.next.length; i++){

        var child_ul = document.createElement("ul");
        var child = obj.next[i];
        parse_data(child,child_ul);

        ul.appendChild(child_ul);
    }

}




